<template>
  <div class="container">
    <div class="btnClose">
      <span class="iconfont iconicon-test"></span>
    </div>
    <div class="logo">
      <span class="iconfont iconnew"></span>
    </div>
    <div>
      <Commoninput
        message="用户名/手机号"
        :regular="/^.{1,8}$/"
        type="text"
        mistake="请输入正确的用户名"
        @getinform="getusername"
      ></Commoninput>
      <!-- <div>{{ mistake }}</div> -->
    </div>

    <div>
      <!-- :regular="/^\w{6,12}$/" 验证密码的正则表达式 ^开头 \w字母或数字，下划线，{6,12}6到12位数，$为结尾 -->
      <Commoninput
        message="密码"
        mistake="请输入正确6到12位的密码数"
        :regular="/^\w{3,12}$/"
        type="password"
        @getinform="getuserpassword"
      ></Commoninput>
    </div>
    <div>
      <Commonbut @click.native="login" name="马上登录"></Commonbut>
      <div class="query" @click="close">没有账号？去注册</div>
    </div>
  </div>
</template>

<script>
// 引入子组件
import Commoninput from "../components/Commoninput .vue";
import Commonbut from "../components/Commonbut";
export default {
  components: { Commoninput, Commonbut },
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    close() {
      // 编程式跳转
      this.$router.push("./register");
      console.log(this.$router);
    },
    // 父组件接收getinform事件，然后getinform事件绑定getusername来记录需求
    // newValue是用户输入的值
    getusername(newValue) {
      this.username = newValue;
    },
    getuserpassword(newValue) {
      this.password = newValue;
    },
    login() {
      this.$axios({
        method: "POST",
        url: "login",
        data: {
          username: this.username,
          password: this.password,
        },
      }).then((res) => {
        // console.log(res.data);
        // 判断用户名和密码为空时
        if (this.username === "" && this.password === "") {
          this.$toast("请输入用户名或者密码");

          // 登陆成功时
        } else if (this.username === "") {
          this.$toast("请输入用户名");
        } else if (this.password === "") {
          this.$toast("请输入密码");
        } else if (res.data.message == "登录成功") {
          this.$toast.loading({
            message: "登录成功,加载中...",
            forbidClick: true,
          });
          // 页面渲染完成后将token,id存起来
          localStorage.setItem("token", res.data.data.token);
          localStorage.setItem("userId", res.data.data.user.id);
          this.$router.push("./");
        } else if (res.data.statusCode === 401) {
          this.$toast("用户不存在");
        }
        console.log(res.data);
      });
    },
  },
};
</script>

<style lang="less" scoped>
// scoped 限定作用域
* {
  margin: 0;
  padding: 0;
}

a {
  color: inherit;
}

a:hover {
  text-decoration: none;
}

ul,
li,
ol {
  list-style: none;
}

.index {
  text-align: center;
}
.logo {
  text-align: center;
  .iconfont {
    font-size: 126/360 * 100vw;
    color: #d81e06;
  }
}
.btnClose {
  .iconfont {
    font-size: 28/360 * 100vw;
  }
}
.query {
  text-align: center;
  padding-top: 16/360 * 100vw;
}
</style>
